package com.github.ghsea.scheduler.zkclient;

import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.zookeeper.data.Stat;

public abstract class NodeChangedListener implements NodeCacheListener {
	private NodeCache cache;

	private Serializer serializer;

	abstract void nodeChanged(String path, Stat stat, Object data) throws Exception;

	@Override
	public void nodeChanged() throws Exception {
		ChildData data = cache.getCurrentData();
		this.nodeChanged(data.getPath(), data.getStat(), serializer.deserialize(data.getData()));
	}

	protected void setCache(NodeCache cache) {
		this.cache = cache;
	}

	protected void setSerializer(Serializer serializer) {
		this.serializer = serializer;
	}

}
